﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace VietNamKey
{
    public partial class ucConvert : UserControl
    {
        ucUnicodeEx UnicodeEx = new ucUnicodeEx();
        public ucConvert()
        {
            InitializeComponent();
            UnicodeEx.Location = new Point(0, 0);
            UnicodeEx.Visible = false;
            this.Controls.Add(UnicodeEx);
            UnicodeEx.BringToFront();
            cboDestination.Items.AddRange(Encodings.List);
            cboSource.Items.AddRange(Encodings.List);
            cboSource.SelectedIndex = cboDestination.SelectedIndex = 0;
        }

        private void btnSwap_Click(object sender, EventArgs e)
        {
            var t = cboSource.SelectedIndex;
            cboSource.SelectedIndex = cboDestination.SelectedIndex;
            cboDestination.SelectedIndex = t;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                if (chbClipboardConvert.Checked)
                {
                    var text = Clipboard.GetText(TextDataFormat.UnicodeText);
                    text = FullConvert(text);
                    Clipboard.SetText(text, TextDataFormat.UnicodeText);
                }
                else
                {
                    var text = File.ReadAllText(txtFileSourse.Text);
                    text = FullConvert(text);
                    File.WriteAllText(txtFileDestination.Text, text, System.Text.Encoding.Unicode);
                }
                MessageBox.Show("Chuyển đổi thành công!");
            }
            catch
            {
                MessageBox.Show("Có lỗi, không chuyển đổi được!");
            }
        }

        private string FullConvert(string text)
        {
            var Destination = (ItemEncoding)cboDestination.SelectedItem;
            var Source = (ItemEncoding)cboSource.SelectedItem;

            if (Source != Encodings.Unicode)
                text = Encodings.Convert(text, Source.Value, Encodings.Unicode.Value);

            if (chbToUnsign.Checked) text = Encodings.ConvertToUnsign(text);
            if (radLower.Checked) text = text.ToLower();
            if (radUpper.Checked) text = text.ToUpper();

            if (Destination != Encodings.Unicode)
                text = Encodings.Convert(text, Encodings.Unicode.Value, Destination.Value);
            return text;
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtFileSourse.Text = dialog.FileName;
            }
        }

        private void btnDestination_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtFileDestination.Text = dialog.FileName;
            }
        }

        private void btnUnicodeEx_Click(object sender, EventArgs e)
        {
            UnicodeEx.Visible = true;
        }
    }
}
